﻿using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using SlamDunk.MES.Domain.Entities.ResourceContext;

namespace SlamDunk.MES.Repository.EntityConfigurations.ResourceContext
{
    /// <summary>
    /// 物料定义实体映射配置
    /// </summary>
    public class MaterialConfig : IEntityTypeConfiguration<Material>
    {
        /// <summary>
        /// 配置
        /// </summary>
        public void Configure(EntityTypeBuilder<Material> builder)
        {
            //配置属性
            builder.HasKey(material => material.Id).IsClustered(false);
            builder.Property(material => material.Number).IsRequired().HasMaxLength(32);
            builder.HasOne(material => material.MaterialPack).WithOne(pack => pack.Material).HasForeignKey<MaterialPack>().IsRequired().OnDelete(DeleteBehavior.Cascade);
            builder.HasOne(x => x.MaterialBarcodeSetting).WithOne(setting => setting.Material).HasForeignKey<MaterialBarcodeSetting>().IsRequired().OnDelete(DeleteBehavior.Cascade);
            builder.HasMany(material => material.RelatedClasses).WithOne(rel => rel.Material).IsRequired().OnDelete(DeleteBehavior.Cascade);

            //配置索引
            builder.HasIndex(material => material.AddedTime).IsUnique(false).IsClustered();
            builder.HasIndex(material => material.Number).IsUnique();
        }
    }
}
